Skip to content

Fix GH-25404: Unwrap by-reference returns forwarded by SPL methods - #25445

Open
Truta446 wants to merge 1 commit into
php:PHP-8.6from
Truta446:spl-unwrap-ref-returns
Open

Truta446 wants to merge 1 commit into
php:PHP-8.6from
Truta446:spl-unwrap-ref-returns

Conversation

@Truta446

Copy link
Copy Markdown
Contributor

Fixes GH-25404

RecursiveIteratorIterator::callHasChildren(), callGetChildren() and RecursiveFilterIterator::hasChildren() pass their return_value straight to the user method. If that method returns by reference, the reference ends up in the return value of a function that is not by-ref, and the assertion after the call in the VM fails.

The fix unwraps the reference after the call, the same way CallbackFilterIterator::accept() and zend_test's zend_call_method_if_exists() already do. zend_return_unwrap_ref() isn't the right fit here: these methods never return by reference, so the reference has to go regardless of how they are called. zend_call_method() in zend_test gets the same fix, since it's the fourth case in the issue.

I also went through the other places that pass return_value to a user call (array_reduce(), ReflectionProperty::getValue() with a by-ref get hook, CallbackFilterIterator, call_user_func()), and they are fine. One thing I noticed while doing that: PDOStatement::fetchAll(PDO::FETCH_FUNC, ...) with a by-ref function puts the reference into the result array, so writing to the array changes the referenced variable. That is the same on 8.4 and 8.5 and doesn't trip any assertion, so I left it out of this PR.

Tested on PHP-8.6 and master (debug + ASan, also with opcache/tracing JIT): ext/spl/tests and ext/zend_test/tests all pass, and both new tests fail without the change.

RecursiveIteratorIterator::callHasChildren(), callGetChildren() and
RecursiveFilterIterator::hasChildren() pass their return_value straight to
the user method. When that method returns by reference, the reference ends
up in the return value of a function that does not return by reference,
which trips the assertion in the VM after the call.

Unwrap the reference, like CallbackFilterIterator::accept() already does.
zend_test's zend_call_method() had the same problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant